﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StairProperty : MonoBehaviour {
    public UnityEngine.UI.Text m_tNumber;
    public UnityEngine.UI.RawImage m_riBG;
    public UnityEngine.UI.Image m_iFill;
    public GameObject m_gEffect;
    public bool m_bIsLive = true,m_bIsEnegy;    
    public int m_iType,m_iType2,m_iEnegy;

    private float m_fTime,m_fTime2;
    public bool m_bEnable=false;
    private MsgBox m_MBquestion;
	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {
        if (m_bEnable && GameData.gi.bIsGameRunning)
        {
            if (m_iType == 2)
            {
                m_iFill.gameObject.SetActive(true);
                if (m_fTime > 0)
                {
                    m_fTime -= Time.deltaTime * GameData.gi.fGameSpeed * GameData.gi.SGR.fGameSpeed;
                    m_iFill.fillAmount = m_fTime / m_fTime2;
                }
                else
                {
                    m_bIsLive = false;
                }

                if (!m_bIsLive)
                {
                    //二秒后清除
                    Destroy(gameObject, 2);
                    gameObject.AddComponent<Rigidbody>();
                    gameObject.GetComponent<BoxCollider>().isTrigger = true;
                    m_bEnable = false;
                    //如果人还在上面，就输了
                    Hashtable adata = new Hashtable();
                    adata["msg"] = gameObject.name;
                    adata["type"] = 2;
                    NotificationCenter.DefaultCenter().PostNotification(this, "PlayerScript_checkLife", adata);
                }
            }
            else if (m_iType == 3)
            {
                m_fTime2 += Time.deltaTime * GameData.gi.fGameSpeed * GameData.gi.SGR.fGameSpeed;
                m_iFill.fillAmount = (m_fTime - m_fTime2) / m_fTime;
                if (m_fTime2 >= m_fTime)
                {
                    m_fTime2 = 0;
                    m_bIsLive = !m_bIsLive;
                    if (m_bIsLive)
                    {
                        //生成火
                        m_gEffect.SetActive(false);
                    }
                    else
                    {
                        Hashtable adata = new Hashtable();
                        adata["msg"] = gameObject.name;
                        adata["type"] = 3;
                        NotificationCenter.DefaultCenter().PostNotification(this, "PlayerScript_checkLife", adata);
                        m_gEffect.SetActive(true);
                    }
                }
            }           
        }        
	}

    public void InitialStair(int type, float t, MsgBox MBquestion = new MsgBox(),int iQuestionId=0,int iAnswerId=0)
    {
        //1:正常的  2：会消失的

        if (GameData.GetRandomRate(GameData.gi.SGR.fEnegyGeneralRate))
        {
            transform.GetChild(3).gameObject.SetActive(true);
            m_bIsEnegy = true;
            m_iEnegy = Random.Range(1, GameData.gi.iEnegyPermax);
            transform.GetChild(3).localScale = new Vector3(1 + m_iEnegy / 10.0f, 1 + m_iEnegy / 10.0f, 1);
        }

        m_iType = type;
        SpriteRenderer sr=gameObject.GetComponent<SpriteRenderer>();
        switch (type)
        {
            case 1:
                m_bIsLive = true;
                sr.sprite = CommonFunctionA.SetSpriteTexture(CommonFunctionA.loadPicture("a"+Random.Range(1,4)));
                break;
            case 2:
                m_bIsLive = true;
                m_fTime = m_fTime2=t;
                if (t == 1)
                {
                    //红色的危险
                    m_iType2 = 1;
                    sr.sprite = CommonFunctionA.SetSpriteTexture(CommonFunctionA.loadPicture("b" + Random.Range(1, 4)));
                }
                else if (t == 3)
                {
                    //黄色的注意
                    m_iType2 = 2;
                    sr.sprite = CommonFunctionA.SetSpriteTexture(CommonFunctionA.loadPicture("c" + Random.Range(1, 4)));
                }
                break;
            case 3:
                m_bIsLive = true;
                m_iFill.gameObject.SetActive(true);
                {
                    sr.sprite = CommonFunctionA.SetSpriteTexture(CommonFunctionA.loadPicture("d" + Random.Range(1, 4)));                    
                }
                m_fTime = t;
                m_fTime2 = 0;
                enableEffect();
                break;
            case 4:
                m_bIsLive = true;
                m_MBquestion = MBquestion;
                {
                    sr.sprite = CommonFunctionA.SetSpriteTexture(CommonFunctionA.loadPicture("e" + Random.Range(1, 4)));
                }
                break;
            case 5:
                {
                    sr.sprite = CommonFunctionA.SetSpriteTexture(CommonFunctionA.loadPicture("e" + Random.Range(1, 4)));
                    m_tNumber.gameObject.SetActive(true);
                    m_riBG.gameObject.SetActive(true);
                    switch(iAnswerId)
                    {
                        case 1:
                            m_tNumber.text = GameData.qi.LstQuestions[iQuestionId].a1;
                            m_bIsLive = true;
                            break;
                        case 2:
                            m_tNumber.text = GameData.qi.LstQuestions[iQuestionId].a2;
                            m_bIsLive = false;
                            break;
                        case 3:
                            m_tNumber.text = GameData.qi.LstQuestions[iQuestionId].a3;
                            m_bIsLive = false;
                            break;
                    }
                    
                }
                break;
            case 6:
                {
                    sr.sprite = CommonFunctionA.SetSpriteTexture(CommonFunctionA.loadPicture("bar3_test2"));
                }
                break;
        }
    }

    public void enableEffect()
    {
        if (m_iType == 4 && !m_bEnable)
        {
            //带问题的
            Hashtable adata = new Hashtable();
            adata["msg"] = m_MBquestion;
            NotificationCenter.DefaultCenter().PostNotification(this, "GameMain_MessageBox", adata);
        }
        m_bEnable = true;
    }
}
